package com.bdqn.controller;

import com.bdqn.pojo.User;
import com.bdqn.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {
    @Resource
    private UserService userService;


    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping("/main")
    public String toMain(){
        return "main";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");
        session.invalidate();
        return "redirect:/login";
    }

    @PostMapping("/login")
    public String doLogin(String usrName, String usrPassword, HttpServletRequest request){
        User user=userService.login(usrName, usrPassword);
        if (user!=null){
            request.getSession().setAttribute("loginUser", user);
            return "redirect:/main";
        }else{
            request.setAttribute("message","登录失败，用户名或密码错误！");
            return "/login";
        }
    }
}
